<?php
/**
 * Created by PhpStorm.
 * User: Donal
 * Date: 16/4/12
 * Time: 下午10:07
 */
namespace Home\Controller;

use Think\Controller;
use Home\Lib\PushManager;

class FriendController extends Controller
{


    public function getAllUsers()
    {
        $User = D('IMUser');
        $count = $User->count();
        $list = $User->order('created')->select();
        $data['status'] = 0;
        $data['data'] = $list;
        $this->ajaxReturn($data);
    }

    function pushApplyMsg($tid, $request_uid, $request_avatar, $request_nickname, $request_msg, $request_type)
    {
        $pushManager = new \Home\Lib\PushManager();
        $pushManager->pushMessageToUid($tid, $pushManager->IGtNotificationTemplateApplyFriend());
        $msg['uid'] = $request_uid;
        $msg['avatar'] = $request_avatar;
        $msg['nickname'] = $request_nickname;
        $msg['msg'] = $request_msg;
        $msg['type'] = $request_type;
        $pushManager->pushTransimisionMessageToUid($tid,
            $pushManager->IGtTransmissionTemplateApplyFriend(json_encode($msg)));
    }

    function applyFriend()
    {
        $uid = I('param.uid');
        $tid = I('param.tid');
        $avatar = I('param.avatar');
        $nickname = I('param.nickname');
        $msg = I('param.msg');

        $FriendShip = D('IMFriendShip');

        $aresult = $FriendShip->where("fid=%d and tid=%d", $uid, $tid)->find();
        if ($aresult) {
            if ($aresult['type'] == 1) {
                $data['status'] = 1;
                $data['msg'] = '已经是好友关系了';
                $this->ajaxReturn($data);
            } else {
                $this->pushApplyMsg($tid, $uid, $avatar, $nickname, $msg, 1);
                $data['status'] = 0;
                $data['msg'] = '申请好友成功';
                $this->ajaxReturn($data);
            }
        } else {
            $params = array(
                'fid' => $uid,
                'tid' => $tid,
                'type' => 0
            );
            $res = $FriendShip->add($params);
            if ($res) {
                $this->pushApplyMsg($tid, $uid, $avatar, $nickname, $msg, 1);
                $data['status'] = 0;
                $data['msg'] = '申请好友成功';
                $this->ajaxReturn($data);
            } else {
                $data['status'] = 2;
                $data['msg'] = '申请好友失败';
                $this->ajaxReturn($data);
            }
        }
    }

    function pushConfirmMsg($tid, $request_uid, $request_type)
    {
        $pushManager = new PushManager();
        $pushManager->pushMessageToUid($tid, $pushManager->IGtNotificationTemplateApplyFriend());
        $msg['uid'] = $request_uid;
        $msg['type'] = $request_type;
        $pushManager->pushTransimisionMessageToUid($tid,
            $pushManager->IGtTransmissionTemplateApplyFriend(json_encode($msg)));
    }

    function confirmFriend()
    {
        $uid = I('param.uid');
        $tid = I('param.tid');
        $FriendShip = D('IMFriendShip');
        $result = $FriendShip->where("fid=%d and tid=%d", $uid, $tid)->find();
        if ($result) {
            if ($result['type'] == 1) {
                $data['status'] = 2;
                $data['msg'] = '已经是好友关系了';
                $this->ajaxReturn($data);
            } else {
                $para['type'] = 1;
                if ($FriendShip->where("fid=%d and tid=%d", $tid, $uid)->save($para)) {
                    $res = $FriendShip->where("fid=%d and tid=%d", $uid, $tid)->save($para);
                    if ($res) {
                        $this->pushConfirmMsg($tid, $uid, 2);
                        $data['status'] = 0;
                        $data['msg'] = '确认好友关系成功';
                        $this->ajaxReturn($data);
                    } else {
                        $data['status'] = 1;
                        $data['msg'] = '确认好友关系失败';
                        $this->ajaxReturn($data);
                    }
                } else {
                    $data['status'] = 1;
                    $data['msg'] = '确认好友关系失败';
                    $this->ajaxReturn($data);
                }
            }
        } else {
            $para['type'] = 1;
            if ($FriendShip->where("fid=%d and tid=%d", $tid, $uid)->save($para)) {
                $params = array(
                    'fid' => $uid,
                    'tid' => $tid,
                    'type' => 1
                );
                $res = $FriendShip->add($params);
                if ($res) {
                    $this->pushConfirmMsg($tid, $uid, 2);
                    $data['status'] = 0;
                    $data['msg'] = '确认好友关系成功';
                    $this->ajaxReturn($data);
                } else {
                    $data['status'] = 1;
                    $data['msg'] = '确认好友关系失败';
                    $this->ajaxReturn($data);
                }
            } else {
                $data['status'] = 1;
                $data['msg'] = '确认好友关系失败';
                $this->ajaxReturn($data);
            }
        }
    }

    public function getFriendInfo()
    {
        $uid = I('param.uid');
        $tid = I('param.tid');
        $name = I('param.tname');
        $User = D('IMUser');
        if ($name) {
            $user = $User->where("name='%s'", $name)->field('password,salt,push_shield_status', true)->find();
        } else {
            $user = $User->where("id=%d", $tid)->field('password,salt,push_shield_status', true)->find();
        }
        if ($user) {
            $user['is_friend'] = 0;
            if ($uid == $tid) {
                $user['is_friend'] = 2;
            } else {
                $FriendShip = D('IMFriendShip');
                $aresult = $FriendShip->where("fid=%d and tid=%d", $uid, $tid)->find();
                if ($aresult) {
                    if ($aresult['type'] == 1) {
                        $user['is_friend'] = 1;
                    }
                }
            }
            $data['status'] = 0;
            $data['data'] = $user;
            $this->ajaxReturn($data);
        } else {
            $data['status'] = 2;
            $data['msg'] = '没有这个用户';
            $this->ajaxReturn($data);
        }
    }

    public function getFriendList()
    {
        $uid = I('param.uid');
        $FriendShip = D('IMFriendShip');
        $fidsFileds = $FriendShip->where("fid=%d and type=1", $uid)->field('tid')->select();
        $fids = array();
        if ($fidsFileds) {
            foreach ($fidsFileds as $fidsFiled) {
                $fids[] = $fidsFiled['tid'];
            }
        }
        if (!empty($fids)) {
            $User = D('IMUser');
            $res = $User->where(array('id' => array("IN", $fids)))->select();
            $data['status'] = 0;
            $data['data'] = $res;
        } else {
            $data['status'] = -1;
            $data['data'] = array();
        }
        $this->ajaxReturn($data);
    }
}